<?php
namespace app\admin\controller;

use app\admin\model\Account as AccountModel;
use app\admin\model\Feedback as FeedbackModel;
use think\Db;
use think\Request;

class Feedback extends Base
{
    public function index()
    {
        $model = new FeedbackModel;

        //搜索时间区间
        $phname = $this->request->param("phname");

        //渲染页面模板

        $model->field('f.id,f.user_id,f.content,f.type_id,f.jpush_at,u.username,f.created_at,f.admin_id,u.id as uid')
            ->alias('f')
            ->where('f.type_id',1)
            ->join('user u','f.user_id = u.id ','left')
            ->group('f.user_id')
            ->order('f.created_at','desc');
        //  ->select();
        //  ->paginate(5);

        if(!empty($phname)){
            $model->where('u.username','like', '%'.$phname.'%');
        }

        $result = $model->paginate(10,false,[
            'query'=>['phname'=>$phname]
        ]);

        //ajax传递数据
        //计算总共记录条数
        $count = $result->total();
        //分页
        $page = $result->render();
        $this->assign('currentPage',$result->currentPage());
        $this->assign('lastPage',$result->lastPage());
        $this->assign('page',$page);
        $this->assign('feedback',$result);
        $this->assign('count',$count);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }

    //回复

    public function reply($user_id)

    {
        try {
            $model = new FeedbackModel;
            //接收ajax传递的值
            $ispost = $this->request->isPost();

            $admin_id = \think\Session::get('admin_id');
            if($ispost) {
                $reply_content = $this->request->post("content");

                $alias = AccountModel::field("jpush_id")->where("id",$user_id)->value("jpush_id");

                //H是24小时制 h是12小时制
                $time= date('Y-m-d H:i:s', time());

                //极光推送通知消息
                $app_key = "633f085eb5564a4a5217ed0e";
                $master_secret = "3741cee06bc58fc210826603";
                $client = new \JPush\Client($app_key, $master_secret);
                $options = array(
                    'apns_production' => True
                );
                //$client = new JPush($app_key, $master_secret);
                $pusher = $client->push();
                //设置所有平台
                $pusher->setPlatform('all');
                //设置受众
                //$pusher->addAllAudience();
                //设置别名推送
                $pusher->addRegistrationId($alias);
                //设置通知方式和内容
                $pusher->setNotificationAlert('您的问题已回复，请登录查看！');
                $pusher->options($options);
                $response = $pusher->send();

                if ($response) {

                    $result = $model->allowField(true)->save(['jpush_at' => $time,'content'=>$reply_content,'user_id'=>$user_id,'admin_id'=>$admin_id,'type_id'=>1]);
                    //$model->allowField(true)->save(['jpush_at' => $time], ['id' => $id]);
                }

                if ($result == 1) {
                    return json([
                        'code' => 200,
                    ]);
                }
            }
        } catch (\Exception $e) {
            return json(['errcode' => 4000, 'message' => $e->getMessage()]);
        }
    }


    //查看数据
    public function view($user_id)
    {
        try {
            $model = new FeedbackModel;

            $result = $model::field("user_id,content,admin_id,jpush_at")->where("user_id",$user_id)->where('type_id','1')->order('created_at','desc')->select();

                if ($result) {
                    return json([
                        'code' => 200,
                        'data'=>['list' => $result],
                    ]);
                }
        } catch (\Exception $e) {
            return json(['errcode' => 4000, 'message' => $e->getMessage()]);
        }
    }
}
